package com.hitqz.robot.biz.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.hitqz.robot.api.business.entity.TicketsEntity;
import com.hitqz.robot.api.business.vo.TicketsVo;
import com.hitqz.robot.biz.mapper.TicketsMapper;
import com.hitqz.robot.biz.service.TicketsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author xupkun
 * @date 2024/6/3
 */
@Service
@AllArgsConstructor
@Slf4j
public class TicketsServiceIml extends ServiceImpl<TicketsMapper, TicketsEntity> implements TicketsService {

    @Override
    public List<TicketsVo> findRecentTickets(Integer limit) {
        List<TicketsVo> ticketsVoList = Lists.newArrayList();
        List<TicketsEntity> ticketsEntities = super.list(Wrappers.<TicketsEntity>query().lambda().orderByDesc(TicketsEntity::getCreateTime).last(" limit " + limit));
        if (ticketsEntities != null) {
            ticketsEntities.stream().forEach(t-> {
                TicketsVo ticketsVo = new TicketsVo();
                BeanUtil.copyProperties(t, ticketsVo, false);
                ticketsVoList.add(ticketsVo);
            });
        }
        return ticketsVoList;
    }

}
